//
//  NewsViewController.m
//  ZhongKeXinHeater
//
//  Created by Kendama on 2017/6/1.
//  Copyright © 2017年 DT. All rights reserved.
//

#import "NewsViewController.h"
//#import "PublicGetMessageListApi.h"
#import "NewsModel.h"
#import "GetMessageListAPI.h"
#import "NewsTableViewCell.h"
#import "NSObject+YYModel.h"

@interface NewsViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic,strong) NSMutableArray *dataSource;
/** 空数据提示 */
@property (nonatomic,strong) UILabel *emptyLabel;
@property (nonatomic,strong) UIImageView *emptyImageView;

@end

@implementation NewsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)refreshBaseControlValue
{
    
    [self.tableView.mj_header beginRefreshing];
//    [self getNewsListWithPage:1];
}

- (void)setNavigationStyle
{
    [self setNavigationBarTitle:MyNSLocalizedString(@"消息通知")];
}

- (void)getNewsListWithPage:(NSInteger)page {
    GetMessageListAPI *api = [[GetMessageListAPI alloc]initWithDic:@{@"device_type":@"1",@"page":[NSString stringWithFormat:@"%ld",(long)page]}];
    [api startWithCompletionBlockWithSuccess:^(__kindof YTKBaseRequest * _Nonnull request) {
        NSDictionary *dict = [request responseJSONObject];
        [self.dataSource removeAllObjects];
        for (NSDictionary *dic in dict[@"data"]) {
            
            NewsModel *model = [NewsModel yy_modelWithDictionary:dic];
            [self.dataSource addObject:model];
        }
        
        if (self.dataSource.count == 0) {
            self.emptyLabel.hidden = NO;
            self.emptyImageView.hidden = NO;
        } else {
            self.emptyLabel.hidden = YES;
            self.emptyImageView.hidden = YES;

        }
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
    } failure:^(__kindof YTKBaseRequest * _Nonnull request) {
        [self.tableView.mj_header endRefreshing];

    }];
    
}

#pragma mark -- 代理
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataSource.count;
}



- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return px1920Height(300);
}

//- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
//    return px1920Height(50);
//}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return px1920Height(100);
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] init];
    
    UILabel *label = [[UILabel alloc] init];
    [view addSubview:label];
    label.textAlignment = NSTextAlignmentCenter;
    label.backgroundColor = [UIColor groupTableViewBackgroundColor];
    label.font = [UIFont systemFontOfSize:15];
    label.textColor = [UIColor darkGrayColor];
    label.layer.cornerRadius = 10;
    [label.layer setMasksToBounds:YES];
//    label.backgroundColor = [UIColor colorWithRed:227/255.0 green:227/255.0 blue:229/255.0 alpha:1];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(view);
        make.height.mas_equalTo(px1920Height(80));
        make.width.mas_equalTo(px1080Width(500));
    }];
    NewsModel *news = self.dataSource[section];
    label.text = [NSString getTimeWithTimestamp:news.ctime format:@"yyyy-MM-dd HH:mm"];
//    label.text = @"2017.01.17";
    return view;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NewsTableViewCell *cell = [NewsTableViewCell cellWithTableView:tableView];
    NewsModel *news = self.dataSource[indexPath.section];
//    NewsModel *news = [[NewsModel alloc] init];
//    news.content = @"第二代上市啦！快去www.taobao.com购买吧~";
    cell.newsModel = news;

    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    return cell;
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//    NewsModel *news = self.dataSource[indexPath.section];
//    if (news.url != nil) {
//        NSString *shopLink = news.url;
//        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:shopLink]];
//    }
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
}

//- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
//{
////    HCDeviceModel *model = self.deviceList[indexPath.section];
//    // 添加一个删除按钮
//    NSMutableArray *array = [[NSMutableArray alloc]init];
//    
//    UITableViewRowAction *deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
//        
//        [tableView reloadData];
//        [[AlertViewManager shareManager] initWithMessage:@"是否确认删除该消息？" cancleButtonTitle:@"取消" OtherButtonsArray:@[@"确认"] clickAtIndex:^(NSInteger buttonIndex, UIAlertController *alertController) {
//            //                NewsModel *news = self.dataSource[indexPath.section];
//            //                PublicDeleteMassageApi *api = [[PublicDeleteMassageApi alloc]initWithDic:@{@"id":news.ids}];
//            //                [api startWithCompletionBlockWithSuccess:^(__kindof YTKBaseRequest * _Nonnull request) {
//            //                    [self.dataSource removeObject:news];
//            //                    [SVProgressHUD showInfoWithStatus:request.responseObject[@"errmsg"]];
//            //                    [self getNewsListWithPage:1];
//            //                } failure:^(__kindof YTKBaseRequest * _Nonnull request) {
//            //                    [SVProgressHUD showErrorWithStatus:request.responseObject[@"errmsg"]];
//            //                }];
//        }];
//    }];
//    
//    [array addObject:deleteRowAction];
//    
//    return array;
//}
//
//
//- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    return YES;
//}
//
//- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
//}


- (void) masLayoutSubview{
    __weak UIView *superView = self.view;
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(superView).with.offset(64);
        make.left.right.equalTo(superView);
        make.bottom.equalTo(superView);
    }];
    
    [self.emptyLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.centerY.equalTo(self.view);
    }];
    
    [self.emptyImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.emptyLabel.mas_top).with.offset(-px1920Height(55));
        make.centerX.equalTo(self.emptyLabel);
        make.width.height.mas_equalTo(px1080Width(400));
    }];

    self.emptyLabel.hidden = YES;
    self.emptyImageView.hidden = YES;
}

#pragma mark -- 懒加载
- (UITableView *)tableView
{
    if(!_tableView)
    {
        UIView *superView = self.view;
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 0, 0) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        MJRefreshStateHeader *header = [MJRefreshStateHeader headerWithRefreshingTarget:self refreshingAction:@selector(getNewsList)];
        [header setTitle:MyNSLocalizedString(@"正在刷新...") forState:MJRefreshStateRefreshing];
        [header setTitle:MyNSLocalizedString(@"正在刷新...") forState:MJRefreshStatePulling];
        [header setTitle:MyNSLocalizedString(@"正在刷新...") forState:MJRefreshStateWillRefresh];
        [header setTitle:MyNSLocalizedString(@"正在刷新...") forState:MJRefreshStateIdle];
        header.lastUpdatedTimeLabel.hidden = YES;
        _tableView.mj_header = header;
//        _tableView.mj_header = [MJRefreshStateHeader headerWithRefreshingBlock:^{
//        }];
        [superView addSubview:_tableView];
    }
    return _tableView;
}

- (void)getNewsList
{
    [self getNewsListWithPage:1];
}

- (NSMutableArray *)dataSource{
    if (!_dataSource) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}

- (UILabel *)emptyLabel{
    
    if (!_emptyLabel) {
        _emptyLabel = [[UILabel alloc]init];
        _emptyLabel.text = @"暂无消息";
        _emptyLabel.font = [UIFont systemFontOfSize:15];
        _emptyLabel.textColor = [UIColor darkGrayColor];
        [self.view addSubview:_emptyLabel];
    }
    return _emptyLabel;
}

- (UIImageView *)emptyImageView
{
    if(!_emptyImageView)
    {
        UIView *superView = self.view;
        _emptyImageView = [[UIImageView alloc]init];
        _emptyImageView.image = [UIImage imageNamed:@"no news"];
        [superView addSubview:_emptyImageView];
    }
    return _emptyImageView;
}


@end
